﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using teavch.Util.SitemapWriter;
using System.Xml;
using System.Xml.Linq;

namespace teavch.Util.SitemapWriterSample
{
	public partial class frmSitemapWriter : Form
	{

		Sitemap sitemap = null;

		public frmSitemapWriter()
		{
			InitializeComponent();
			this.FillSearchEngine();
		}

		private void FillSearchEngine()
		{ 
			string[] searchEngine = Enum.GetNames(typeof( SupportedSearchEngine));
			foreach(string s in searchEngine)
			{
				this.cboSearchEngine.Items.Add( s );
				this.cboSearchEngine.SelectedIndex = this.cboSearchEngine.Items.Count - 1;
			}
		}

		private void btnClose_Click( object sender, EventArgs e )
		{
			//string sXmlLocation = System.Environment.GetFolderPath( System.Environment.SpecialFolder.ApplicationData ) + "\\" + Application.ProductName;
			//if ( !Directory.Exists( sXmlLocation ) )
			//	Directory.CreateDirectory( sXmlLocation );
			//sXmlLocation = sXmlLocation + "\\~sitemap.xml";//  sFolder
			//StreamReader xml = new StreamReader( sXmlLocation, true );
			//XDocument doc = XDocument.Load( xml );
			//xml.Close();
			//xml.Dispose();
			//XElement element = new XElement( "url", new XElement( "loc", "http://www.techblogster.teavch.com/tool/show/web-ui-automation_new" ) );
			////element.RemoveAnnotations( typeof( XNamespace ) );
			//doc.Root.Add( element );
			//doc.Save( sXmlLocation );

			//xml = new StreamReader( sXmlLocation, true );
			//string sXml = xml.ReadToEnd();
			//xml.Close();
			//xml.Dispose();
			//sXml = sXml.Replace( " xmlns=\"\"", "" );
			//File.WriteAllText( sXmlLocation, sXml );

			//xml = new StreamReader( sXmlLocation, true );
			//doc = XDocument.Load( xml );
			//xml.Close();
			//xml.Dispose();
			//doc.Save( sXmlLocation );

			Application.Exit();
		}

		private void btnGoAsync_Click( object sender, EventArgs e )
		{


			try
			{

				if ( cboSearchEngine.SelectedItem.ToString() == "" )
				{
					MessageBox.Show( "Plesae select the type of Search Engine" );
					return;
				}

				btnGoAsync.Enabled = false;
				Crawler crawler = new Crawler();

				crawler.IncludeOptions.IncludeImage = true;
				//crawler.IncludeLastModifiedTag = true;
				//crawler.IncludeChangeFrequencyTag = true;
				//crawler.IncludePriorityTag = true;
				crawler.ExcludeUrlsContaining = new List<string>();
				crawler.ExcludeUrlsContaining.Add( "/account" );
				crawler.ExcludeUrlsContaining.Add( "/authentication" );

				crawler.ExcludeImageUrlsContaining = new List<string>();
				crawler.ExcludeImageUrlsContaining.Add( "/images/" );
				crawler.ExcludeImageUrlsContaining.Add( "/account-images/" );

				crawler.CrawlingCompleted += new CrawlingCompletedEventHandler( CrawlingCompleted_CrawlingCompletedEventHandler );
				crawler.CrawlingStatusChanged += new CrawlingStatusChangedEventHandler( CrawlingStatusChanged_CrawlingStatusChangedEventHandler );

				crawler.CrawlAsync( txtUrl.Text, txtUrl.Text );
			}
			catch ( Exception ex )
			{
				lblMsg.Text = ex.Message;
				btnGoAsync.Enabled = true;
			}
		}

		private void CrawlingCompleted_CrawlingCompletedEventHandler( object sender, CrawlingCompletedEventArgs e )
		{
			this.sitemap = e.Sitemap;

			try
			{
				string sXmlLocation = System.Environment.GetFolderPath( System.Environment.SpecialFolder.ApplicationData ) + "\\" + Application.ProductName;
				if ( !Directory.Exists( sXmlLocation ) )
					Directory.CreateDirectory( sXmlLocation );
				sXmlLocation = sXmlLocation + "\\~sitemap.xml";//  sFolder
				if ( File.Exists( sXmlLocation ) )
					File.Delete( sXmlLocation );
				string sSearchEngine = "";
				cboSearchEngine.Invoke( 
					new MethodInvoker( 
					delegate 
					{ 
						sSearchEngine = cboSearchEngine.SelectedItem.ToString(); 
					} ) );
				string sXml = e.Sitemap.GetXml( ( SupportedSearchEngine ) Enum.Parse( typeof( SupportedSearchEngine ), sSearchEngine ) );
				File.WriteAllText( sXmlLocation, sXml );
				StreamReader xml = new StreamReader( sXmlLocation, true );
				XDocument doc = XDocument.Load( xml );
				xml.Close();
				xml.Dispose();
				doc.Save( sXmlLocation );

				System.Threading.Thread.Sleep( 1000 );

				wbSitemap.Url = new Uri( sXmlLocation );
				wbSitemap.Refresh( WebBrowserRefreshOption.Completely );
				btnGoAsync.Invoke( new Action( () => btnGoAsync.Enabled = true ) );

			}
			catch ( IOException ioe )
			{
				if ( ioe.Message.Contains( "" ) )
				{ 
					
				}
				btnGoAsync.Invoke( new Action( () => btnGoAsync.Enabled = true ) );
			}
		}

		private void CrawlingStatusChanged_CrawlingStatusChangedEventHandler( object sender, CrawlingStatusChangedEventArgs e )
		{
			lblMsg.Invoke( new Action( () => lblMsg.Text = e.StatusMessage ) );
		}

		private void btnSaveAs_Click( object sender, EventArgs e )
		{
			SaveFileDialog sfd = new SaveFileDialog();
			sfd.CheckPathExists = true;
			sfd.FileName = "sitemap.xml";
			sfd.Filter = "XML Files | *.xml";
			sfd.Title = "Save generated sitemap";
			sfd.ValidateNames = true;
			sfd.InitialDirectory = System.Environment.GetFolderPath( Environment.SpecialFolder.MyDocuments );
			sfd.AutoUpgradeEnabled = true;
			sfd.ShowDialog( this );
			lblMsg.Text = "";
			if ( sfd.FileName != "sitemap.xml" )
			{
				string sXmlLocation = System.Environment.GetFolderPath( System.Environment.SpecialFolder.ApplicationData ) + "\\" + Application.ProductName;
				if ( !Directory.Exists( sXmlLocation ) )
					Directory.CreateDirectory( sXmlLocation );
				sXmlLocation = sXmlLocation + "\\~sitemap.xml";//  sFolder
				//string sXml = this.sitemap.GetXml( SupportedSearchEngine.Google );
				System.IO.File.Copy( sXmlLocation, sfd.FileName, true );
				lblMsg.Text = "File saved at " + sfd.FileName;
			}
		}

		private void btnRefreshXml_Click( object sender, EventArgs e )
		{
			try
			{
				if ( this.sitemap != null )
				{
					string sXmlLocation = System.Environment.GetFolderPath( System.Environment.SpecialFolder.ApplicationData ) + "\\" + Application.ProductName;
					if ( !Directory.Exists( sXmlLocation ) )
						Directory.CreateDirectory( sXmlLocation );
					sXmlLocation = sXmlLocation + "\\~sitemap.xml";//  sFolder
					if ( File.Exists( sXmlLocation ) )
						File.Delete( sXmlLocation );
					string sXml = this.sitemap.GetXml( SupportedSearchEngine.Bing );
					File.WriteAllText( sXmlLocation, sXml );
					wbSitemap.Url = new Uri( sXmlLocation );
					wbSitemap.Refresh( WebBrowserRefreshOption.Completely );
				}
			}
			catch ( IOException ioe )
			{
				if ( ioe.Message.Contains( "" ) )
				{

				}
			}
		}

	}
}